package com.wanmait.shoppingsys.controller.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.wanmait.shoppingsys.dao.EvaluationDAO;
import com.wanmait.shoppingsys.dao.SpecialtyDAO;
import com.wanmait.shoppingsys.dao.TradeDAO;
import com.wanmait.shoppingsys.vo.Evaluation;
import com.wanmait.shoppingsys.vo.Specialty;
import com.wanmait.shoppingsys.vo.Trade;
import com.wanmait.shoppingsys.vo.UserInfo;
@WebServlet("/EvaluationController")
public class EvaluationController extends HttpServlet {
	
	private EvaluationDAO evaluationDAO=EvaluationDAO.getDAO();
	private SpecialtyDAO specialtyDAO=SpecialtyDAO.getDAO();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String action = request.getParameter("action");
		
		if(action==null) { return; }
		 
		switch(action) {
		case "add":          //评论
			this.add(request, response);
			break;
		case "listComment":       //评论列表
			this.listComment(request, response);
			break;
		case "listint":         //商品详情
			this.listint(request, response);
			break;
		}
	}
	protected void listComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		String specialtyIdStr = request.getParameter("specialtyId");
		
		Integer specialtyId = Integer.parseInt(specialtyIdStr);
		
		List<Evaluation> evaluations = evaluationDAO.findBySpecialtyId(specialtyId);

		
		request.setAttribute("evaluations", evaluations);
		request.getRequestDispatcher("/specialty/evaluation.jsp").forward(request, response);
	}
	protected void listint(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		String specialtyIdStr = request.getParameter("specialtyId");
		
		Integer specialtyId = Integer.parseInt(specialtyIdStr);
		Specialty specialty = specialtyDAO.findById(specialtyId);
		request.setAttribute("specialty",  specialty);
		request.getRequestDispatcher("/specialty/introduce.jsp").forward(request, response);
	}
	
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		  UserInfo userinfo = (UserInfo)request.getSession().getAttribute("userinfo");
		  if(userinfo==null) { return;}
		  Integer userinfoId = userinfo.getId();
		  
		  
//		  UserInfo userInfo = (UserInfo)request.getSession().getAttribute("userinfo");
//		  Integer userinfoId1 = userinfo.getId();
		 
		String specialtyIdStr = request.getParameter("specialtyId");
		Integer specialtyId = Integer.parseInt(specialtyIdStr);
		
		ArrayList<Trade> trades = (ArrayList<Trade>)TradeDAO.getDAO().findall();
		
		int k=0;
		for(int i=0;trades!=null&&i<trades.size();i++) {
			Trade trade = trades.get(i);
			if(trade.getSpecialtyId()==specialtyId) {
				k++;
			}
		}
		if(k>=1) {
			String content = request.getParameter("content");
			Evaluation evaluation=new Evaluation();
			evaluation.setContent(content);
			evaluation.setSpecialtyId(specialtyId);
			evaluation.setUserId(userinfoId);
	
			evaluationDAO.insert(evaluation);
		}else {return;}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
